<template>
  <div class="app-container">
    <!--条件搜索表单-->
    <el-row>
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="searchObj.name" placeholder="配置名称" />
        </el-form-item>
        <el-form-item>
          <el-select v-model="searchObj.isDefault" placeholder="是否默认" clearable @change="getAll()">
            <el-option :value="1" label="是" />
            <el-option :value="0" label="否" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="getAll()">查询</el-button>
          <el-button icon="el-icon-refresh" @click="reset()">清空</el-button>
        </el-form-item>
      </el-form>
    </el-row>

    <!-- 工具栏 -->
    <el-row>
      <el-button v-if="hasPerm('sys:file-config:save')" type="primary" icon="el-icon-plus" @click="openEdit(null)">新增</el-button>
      <el-button v-if="hasPerm('sys:file-config:batch-delete')" type="danger" icon="el-icon-delete" :disabled="ids.length < 1" @click="batchDelete">
        批量删除
      </el-button>
    </el-row>

    <!-- banner列表 :data="list"表示遍历list数组 stripe条纹 -->
    <el-table
      v-loading="loading"
      element-loading-text="正在努力加载数据中..."
      :data="page.records"
      :border="true"
      stripe
      style="width: 100%; margin: 15px 0px"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" fixed="left" width="55" />
      <el-table-column type="index" fixed="left" width="80" label="序号" />
      <el-table-column prop="name" fixed="left" label="配置名称" min-width="120" sortable show-overflow-tooltip />
      <el-table-column prop="storageType" label="存储类型" min-width="120" />
      <el-table-column prop="endpoint" label="访问地址" min-width="120" sortable show-overflow-tooltip />
      <el-table-column prop="bucketName" label="存储桶名称" min-width="120" sortable show-overflow-tooltip />
      <el-table-column prop="secretKey" label="密钥" min-width="120" show-overflow-tooltip />
      <el-table-column prop="accessKey" label="访问密钥" min-width="120" show-overflow-tooltip />
      <el-table-column prop="isDefault" label="是否默认" min-width="120" sortable>
        <template v-slot="{ row }">
          <el-switch v-model="row.isDefault" :active-value="1" :inactive-value="0" @change="handleStatusChange(row)" />
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注" min-width="120" show-overflow-tooltip />
      <el-table-column prop="createTime" label="创建时间" min-width="120" sortable show-overflow-tooltip />

      <!-- 操作 -->
      <el-table-column v-if="hasPerm('sys:file-config:update', 'sys:file-confg:delete')" label="操作" width="150" fixed="right" align="center">
        <template slot-scope="scope">
          <el-link v-if="hasPerm('sys:file-config:update')" type="success" icon="el-icon-edit" @click="openEdit(scope.row)">编辑</el-link>
          <el-divider v-if="hasPerm('sys:file-config:delete')" direction="vertical" />
          <el-link v-if="hasPerm('sys:file-config:delete')" type="danger" icon="el-icon-remove" @click="remove(scope.row.id)">删除</el-link>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页导航栏 -->
    <div>
      <el-pagination
        :background="true"
        :current-page="pIndex"
        :page-size="pSize"
        :total="page.total"
        :page-sizes="[1, 3, 6, 9, 12, 15, 18]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        @current-change="getAll"
        @size-change="handleSizeChange"
      />
    </div>

    <!-- 表单 -->
    <el-dialog :title="dialogTitle" :visible.sync="visible" @close="close('fileConfigForm')">
      <el-form ref="fileConfigForm" :model="fileConfig" :rules="rules" label-width="80px">
        <el-row :gutter="10">
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="ID" prop="id" :hidden="true">
              <el-input v-model="fileConfig.id" disabled />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="配置名称" prop="name">
              <el-input v-model="fileConfig.name" placeholder="请输入配置名称" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="存储类型" prop="storageType">
              <el-input v-model="fileConfig.storageType" placeholder="请输入存储类型" />
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="访问地址" prop="endpoint">
              <el-input v-model="fileConfig.endpoint" placeholder="请输入访问地址" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="访问密钥" prop="accessKey">
              <el-input v-model="fileConfig.accessKey" placeholder="请输入访问密钥" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="加密密钥" prop="secretKey">
              <el-input v-model="fileConfig.secretKey" placeholder="请输入加密密钥" show-password />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="桶名称" prop="bucketName">
              <el-input v-model="fileConfig.bucketName" placeholder="请输入桶名称" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="地域" prop="region">
              <el-input v-model="fileConfig.region" placeholder="请输入地域" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="前缀" prop="basePath">
              <el-input v-model="fileConfig.basePath" placeholder="请输入前缀" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
            <el-form-item label="是否默认" prop="isDefault">
              <el-radio-group v-model="fileConfig.isDefault" placeholder="是否默认">
                <el-radio :label="1" border>是</el-radio>
                <el-radio :label="0" border>否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="备注" prop="remark">
              <el-input v-model="fileConfig.remark" :rows="3" type="textarea" placeholder="请输入备注" maxlength="255" show-word-limit />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button @click="close('fileConfigForm')">取 消</el-button>
        <el-button type="primary" @click="save('fileConfigForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 引入api接口定义的js文件
import fileConfigApi from '@/api/sys/file-config'

export default {
  data() {
    // 定义变量和初始值
    return {
      loading: false, // 加载状态
      pIndex: 1, // 当前页
      pSize: 6, // 每页显示记录数
      searchObj: {}, // 条件封装对象
      page: [], // 列表
      visible: false, // 表单显示
      ids: [],

      // 表单
      dialogTitle: '',
      fileConfig: {
        id: undefined,
        name: '',
        storageType: '',
        basePath: '',
        endpoint: '',
        accessKey: '',
        secretKey: '',
        bucketName: '',
        region: '',
        isDefault: 0,
        remark: ''
      },

      // 验证规则
      rules: {
        name: [{ required: true, message: '配置名称不能为空', trigger: 'blur' }],
        endpoint: [{ required: true, message: '访问地址不能为空', trigger: 'blur' }],
        accessKey: [{ required: true, message: '访问密钥不能为空', trigger: 'blur' }],
        secretKey: [{ required: true, message: '加密密钥不能为空', trigger: 'blur' }],
        bucketName: [{ required: true, message: '桶名称不能为空', trigger: 'blur' }],
        storageType: [{ required: true, message: '存储类型不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getAll()
  },
  methods: {
    // 加载列表数据
    getAll(current = 1) {
      this.loading = true
      this.pIndex = current
      fileConfigApi
        .getPage(this.pIndex, this.pSize, this.searchObj)
        .then((response) => {
          this.page = response.data
        })
        .finally(() => {
          this.loading = false
        })
    },

    // 复选框选中
    handleSelectionChange(r) {
      // 得到选中的id
      this.ids = r.map((v) => v.id)
    },

    // 改变页码
    handleSizeChange(pageSize) {
      this.pSize = pageSize
      this.getAll()
    },

    // 改变状态
    handleStatusChange(row) {
      const text = row.isDefault === 1 ? '默认' : '停用'
      this.$confirm(`此操作将修改 '${row.name}' 状态为 '${text}', 是否继续?`, '存储配置状态修改', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          fileConfigApi
            .updateStatus(row.id, row.isDefault)
            .then((resp) => {
              this.$message.success('操作成功！')
              this.getAll()
            })
            .catch(() => {
              row.isDefault = row.isDefault === 0 ? 1 : 0
            })
        })
        .catch(() => {
          row.isDefault = row.isDefault === 0 ? 1 : 0
        })
    },

    // 删除
    remove(id) {
      this.$confirm('此操作将永久删除存储配置信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        fileConfigApi.deleteById(id).then((resp) => {
          this.$message.success('删除成功!')
          this.getAll()
        })
      })
    },

    // 打开编辑
    openEdit(row) {
      this.dialogTitle = '新增存储配置'
      this.visible = true
      this.fileConfig.priority = this.page.total + 2

      if (row != null) {
        this.dialogTitle = '编辑存储配置'
        fileConfigApi.getById(row.id).then((res) => {
          Object.assign(this.fileConfig, res.data)
        })
      }
    },

    // 关闭表单
    close(form) {
      this.visible = false
      this.$refs[form].resetFields()
    },

    // 保存
    save(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          const apiCall = this.fileConfig.id ? fileConfigApi.update(this.fileConfig) : fileConfigApi.save(this.fileConfig)
          apiCall.then(() => {
            this.$message.success('操作成功')
            this.close(form)
            this.getAll()
          })
        }
      })
    },

    // 批量删除
    batchDelete() {
      if (this.ids.length > 0) {
        this.$confirm('此操作将永久删除存储配置信息,是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          // 确定执行then方法
          .then(() => {
            fileConfigApi.batchDelete(this.ids).then((resp) => {
              this.$message.success('删除成功！')
              this.getAll()
            })
          })
          .catch(() => {
            this.$message.info('已取消删除！')
          })
      } else {
        this.$message.warning('至少选择一条数据!')
      }
    },

    // 重置
    reset() {
      this.searchObj = {}
      this.getAll()
    }
  }
}
</script>

<style></style>
